/**
 * 
 */
package com.swg.cocobox;

import java.util.EventObject;
import java.util.LinkedList;
import java.util.List;

/**
 * @author satriaprayoga
 *
 */
public abstract class AbstractEventProcessor<E extends EventObject> implements EventProcessor<E> {

	private List<E> events;
	
	public AbstractEventProcessor() {
		events=new LinkedList<E>();
	}
	
	public void queueEvent(E event) {
		synchronized (events) {
			events.add(event);
			events.notify();
		}
	}
	
	@Override
	public void run() {
		E event;
		while(true){
			synchronized (events) {
				while(events.isEmpty()){
					try {
						events.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				event=events.remove(0);
			}
			onEvent(event);
		}
	}
}
